﻿package org.openChart.meta.manager 
{
	import org.openChart.meta.data.DataElement;
	import org.openChart.meta.data.DataGroup;
	import org.openChart.meta.data.GroupSet;
	import org.openChart.meta.Style;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class XmlDataLoader extends XmlBase
	{
		private var xml:XML = null;
		private var file:String = null;
		public function XmlDataLoader( file:String = null ) 
		{
			if (file)
				this.file = file;
		}
		
		public function loadFromFile ():void
		{
			var loader:URLLoader = new URLLoader();
			
            loader.addEventListener(Event.COMPLETE, xmlLoaded);
            //loader.addEventListener(Event., openHandler);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            var request:URLRequest = new URLRequest(file);
            try {
                loader.load(request);
            } catch (error:Error) {
				exception();
            }
		}
		
		
		public function xmlLoaded( event:Event ):void
		{
			var loader:URLLoader = URLLoader(event.target);
			var xmlString:String = new String(loader.data);
			xml = new XML(xmlString);
			dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
		}
		
		public function buildUpLabels( xml:XML = null):Array
		{
			var labels:Array = null;
			
			if (xml.labels[0])
			{
				var object:Object = CSSLoader.instance().getStyle(CSSLoader.axis_2d_labels);
				labels = this.buildLabels(xml.labels[0], new Style(object));
			}
			return labels;
		}
		
		public function buildGroupSet( xml:XML, chartType:String ):GroupSet
		{
			var object:Object = CSSLoader.instance().getStyle(CSSLoader.default_element_style);
			var style:Style = buildStyle(xml, new Style(object) );
			var groupSet:GroupSet = new GroupSet( style );
			
			var i:uint = 0;
			for each ( var row:XML in xml.row)
			{
				i++;
				var rowStyle:Style = new Style();
				if (chartType.indexOf("pie") == -1)
					rowStyle.color = rowStyle.selectedColor = CSSLoader.instance().getRowColor();
				rowStyle = buildStyle(row, rowStyle);
				var group:DataGroup = buildGroup(row, rowStyle, chartType);
				groupSet.addGroup( group );
			}
			return groupSet;
		}
		
		public function buildGroup( xml:XML, parentStyle:Style, chartType:String):DataGroup
		{
			
			var group:DataGroup = new DataGroup(parentStyle);
			var object:Object = CSSLoader.instance().getStyle(CSSLoader.default_element_style);
			group.label = xml.@name;
			//trace("Label " + group.label);
			var i:uint = 0;
			for each ( var cell:XML in xml.cell)
			{
				i++;
				var cellStyle:Style = new Style(object, parentStyle);
				//trace(this + " alpha=" + cellStyle.alpha)
				if ( chartType.indexOf("pie") >= 0 )
					cellStyle.color = cellStyle.selectedColor = CSSLoader.instance().getRowColor();
				cellStyle = buildStyle(cell, cellStyle);
				//race(this + " alpha=" + cellStyle.alpha + " xml alpha=" + cell.@alpha)
				var element:DataElement = new DataElement(cellStyle, new Number(cell));
				group.addElement(element);
			}
			return group;
		}
		
		private function ioErrorHandler( event:IOErrorEvent ):void
		{
			dispatchEvent(new LoaderEvent(LoaderEvent.FAILED));
		}
		
		private function exception():void
		{
			dispatchEvent(new LoaderEvent(LoaderEvent.EXCEPTION));
		}
	}
	
}